package com.jiaai.shop.store.service.impl;

import com.jiaai.shop.store.entity.SUser;
import com.jiaai.shop.store.mapper.SUserMapper;
import com.jiaai.shop.store.service.ISUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jiaai.shop.util.MD5;
import com.jiaai.wgj.util.exception.LoginException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 后台管理员表 服务实现类
 * </p>
 *
 * @author
 * @since 2018-04-01
 */
@Service
public class SUserServiceImpl extends ServiceImpl<SUserMapper, SUser> implements ISUserService {

    @Override
    public Map login(String mId, String pwd) {
        boolean flg=false;
        if (!mId.equals("0")){
            String password=baseMapper.login(mId);
            if (password == null)throw new LoginException(201,"NOUSER");
            if(!password.equals(MD5.MD5Encode(pwd)))throw new LoginException(204,"errorPass");
            flg=true;
            Map map=new HashMap();
            map.put("state",flg);
            map.put("userName",baseMapper.userName(mId));
            return map;
        }
        throw new LoginException(203,"error");
    }

    @Override
    public boolean rege(SUser sUser) throws Exception{
        boolean flg=false;
        try {
            sUser.setPassword(MD5.MD5Encode(sUser.getPassword()));
            baseMapper.insert(sUser);
            flg=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flg;
    }
}
